import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/AppRouter.dart';
import 'package:huajianshe_app/common/C.dart';
import 'package:huajianshe_app/common/TS.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/pages/main/MomentItem.dart';
import 'package:huajianshe_app/pages/search/SearchPage.dart';
import 'package:huajianshe_app/widget/appbar/HomeSearchAppBar.dart';
import 'package:huajianshe_app/widget/BgImageLayout.dart';
import 'package:huajianshe_app/widget/RoundedUnderlineTabIndicator.dart';

import 'MomentListPage.dart';

class HomePage extends StatefulWidget {

  ValueNotifier<int>? mainIndexNotifier; // 首页底部5个tab

  HomePage({Key? key, this.mainIndexNotifier}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends BaseState<HomePage> with SingleTickerProviderStateMixin {

  late TabController tabController;
  late PageController pageController;
  // late ValueNotifier<int> currentIndex;
  late int index;
  
  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 2, vsync: this);
    pageController = PageController(initialPage: 0);
    index = 0;
    // currentIndex = ValueNotifier(index);
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  void onPageChange(int index) {
    tabController.animateTo(index);
  }
  
  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      BgImageLayout(bgColor: C.grayF6, imageHeight: 200, imageUrl: 'bg_main', children: [
        Column(mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [
            HomeSearchAppBar((){ AppNavigator.push(context, RouteName.SEARCH, arguments:
            {'searchType': SearchType.moment, 'momentType': pageController.page == 0
                ? MomentType.MOMENT_TYPE_FRIEND : MomentType.MOMENT_TYPE_RECOMMENDATION}); }),
            H(10),
            Padding(padding: EdgeInsets.only(left: 15, right: 15), child:
              TabBar(
                onTap: (index) { pageController.jumpToPage(index); },
                  labelPadding: EdgeInsets.only(left: 6, right: 6, bottom: 3),
                  indicatorWeight: 1,
                  indicatorSize: TabBarIndicatorSize.label,
                  indicator: RoundedUnderlineTabIndicator(
                    insets: EdgeInsets.only(top: 0, bottom: 0),
                    borderSide: BorderSide(width: 1, color: Colors.white),
                  ),
                  isScrollable: true,
                  controller: tabController,
                  labelColor: Colors.white,
                  unselectedLabelColor: Colors.white,
                  labelStyle: TS(21, Colors.white, fontWeight: FontWeight.bold),
                  unselectedLabelStyle: TS(17, Colors.white),
                  tabs: [Text('好友'), Text('推荐')]
              )
            ),
            H(10),
            Expanded(child:
              Padding(padding: EdgeInsets.only(left: 0, right: 0), child:
                PageView.builder(
                    key: const Key('pageView'),
                    itemCount: 2,
                    onPageChanged: onPageChange,
                    controller: pageController,
                    // itemBuilder: (_, int index) => Text("1"),
                    itemBuilder: (_, int index) => MomentListPage(momentType: index == 0
                        ? MomentType.MOMENT_TYPE_FRIEND : MomentType.MOMENT_TYPE_RECOMMENDATION, mainIndexNotifier: widget.mainIndexNotifier)
                )
              )
            )
        ])
      ],
        floatingActionButton: Padding(padding: EdgeInsets.only(bottom: 75),child:FloatingActionButton(
          backgroundColor: Colors.transparent,
          child: LoadImage('send_moment'),
          // child: Container(width: 52, height: 52, decoration: BoxDecoration(shape: BoxShape.circle, gradient:
          //   LinearGradient(colors: [Color(0xFFFE2E2E), Color(0xFFF78C58)], begin: Alignment.topCenter, end: Alignment.bottomCenter),
          // ), child: Icon(Icons.add),),
          onPressed: () { AppNavigator.push(context, RouteName.PREPOST); },
        ))
      ),
    ]);

  }
}